﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PTA.NET
{
    public class CauTaoCacThang
    {
        //////////KHONG = 0,
        //////////SAI = 1,
        //////////DUNG = 2,


        // L
        //public static int[][] L = new int[][] { 
        //                                        new int[] {}, 
        //                                        new int[] {}, 
        //                                        new int[] {16, 29, 41, 51, 77, 93, 102, 107, 123, 139, 153, 183, 203, 232, 260 }
        //                                    };

        //// F
        ////public static int[] F_DUNG = { 18, 20, 30, 36, 42, 48, 54, 60, 66, 72, 84, 96, 114, 134, 144, 150, 156, 162, 166, 180, 198, 216, 228, 234, 240, 246, 252, 258, 264, 270, 282, 288, 294, 300, 306, 312, 324, 336, 349, 355, 361 };
        ////public static int[] F_SAI = { 6, 12, 78, 90, 102, 108, 120, 126, 132, 174, 186, 192, 204, 210, 222, 276, 318, 330, 343 };

        
        //public static int[][] F = new int[][] { 
        //                            new int[] {}, 
        //                            new int[] { 6, 12, 78, 90, 102, 108, 120, 126, 132, 174, 186, 192, 204, 210, 222, 276, 318, 330, 343 },
        //                            new int[] { 18, 20, 30, 36, 42, 48, 54, 60, 66, 72, 84, 96, 114, 134, 144, 150, 156, 162, 166, 180, 198, 216, 228, 234, 240, 246, 252, 258, 264, 270, 282, 288, 294, 300, 306, 312, 324, 336, 349, 355, 361 }
        //                        };

                                                 

        //public static int TinhDiem(Questions[] QuestionList, int[] CauTruc, string TraLoi)
        //{
        //    int diem = 0;
        //    for (int index = 0; index < CauTruc.Count(); index++)
        //    {
        //        int STT = CauTruc[index];

        //        if (QuestionList[STT].Answer == TraLoi)
        //        {
        //            diem++;
        //        }
        //    }
        //    return diem;
        //}

        //public static int TinhDiem2(Questions[] QuestionList, int[][] CauTruc, int TraLoi)
        //{
        //    int diem = 0;
        //    for (int iTraLoi = 0; iTraLoi < 3; iTraLoi++)
        //    {
        //        for (int index = 0; index < CauTruc[iTraLoi].Count(); index++)
        //        {
        //            int STT = CauTruc[iTraLoi][index];

        //            if (QuestionList[STT].Answer == ((TRA_LOI)iTraLoi).ToString())
        //            {
        //                diem++;
        //            }
        //        }
        //    }
        //    return diem;
        //}

        public static int TinhDiemTheoTraLoi(Questions[] QuestionList, string[] CauTruc)
        {
            int diem = 0;

            // Cột đầu tiên là tên loại câu hỏi: KHONG, SAI, DUNG
            // VD: DUNG,16, 29, 41, 51, 77, 93, 102, 107, 123, 139, 153, 183, 203, 232, 260 
            for (int index = 1; index < CauTruc.Count(); index++)
            {
                if (CauTruc[index] == "") continue;

                int STT = int.Parse(CauTruc[index]);

                if (QuestionList[STT].Answer.ToLower() == CauTruc[0].ToLower())
                {
                    diem++;
                }
            }
            return diem;
        }



        public static int? TinhDiem3(String Sex, Questions[] QuestionList, string ThangDiem)
        {
            int? diem = 0;
            if (QuestionList == null || ThangDiem == null) return null;



            if (ThangDiem.Equals("VRIN"))
            {
                //string[] a = File.ReadAllLines(".\\Data\\CauTaoThangDiem\\" + Sex + "\\" + ThangDiem);
                diem = TinhDiem_VRIN(Sex, QuestionList, ThangDiem);
            }
            else if (ThangDiem.Equals("TRIN"))
            {
                diem = TinhDiem_TRIN(Sex, QuestionList, +1);
                diem += TinhDiem_TRIN(Sex, QuestionList, -1);
                
                // Cộng 9 cho TRIN
                diem += 9;
            }
            else
            {
                string[] a = File.ReadAllLines(".\\Data\\CauTaoThangDiem\\" + Sex + "\\" + ThangDiem);

                // ĐÚNG
                diem += TinhDiemTheoTraLoi(QuestionList, a[0].Split(','));

                // SAI
                diem += TinhDiemTheoTraLoi(QuestionList, a[1].Split(','));
            }
            return diem;
        }

        public static int? TinhDiem_VRIN(String Sex, Questions[] QuestionList, string ThangDiem)
        {
            int diem = 0;
            if (QuestionList == null || ThangDiem == null) return null;

            string[] a = File.ReadAllLines(".\\Data\\CauTaoThangDiem\\" + Sex + "\\" + ThangDiem);
                        
            for (int index = 0; index < a.Count(); index++)
            {
                // Ví dụ: 3,DUNG#39,SAI
                if (a[index] == "") continue;

                string[] cap = a[index].Split('#');

                // Câu đầu tiên
                int STT = int.Parse(cap[0].Split(',')[0]);
                string TraLoi = cap[0].Split(',')[1];

                if (QuestionList[STT].Answer.ToLower() == TraLoi.ToLower())
                {
                    // Câu thứ 2
                    int STT2 = int.Parse(cap[1].Split(',')[0]);
                    string TraLoi2 = cap[1].Split(',')[1];
                    if (QuestionList[STT2].Answer.ToLower() == TraLoi2.ToLower())
                    {
                        diem++;
                    }
                }
            }
            return diem;

        }

        public static int? TinhDiem_TRIN(String Sex, Questions[] QuestionList, int type)
        {
            int diem = 0;
            string[] a = File.ReadAllLines(".\\Data\\CauTaoThangDiem\\" + Sex + "\\TRIN_" + type.ToString());

            for (int index = 0; index < a.Count(); index++)
            {
                // Ví dụ: 3,DUNG#39,SAI
                if (a[index] == "") continue;

                string[] cap = a[index].Split('#');

                // Câu đầu tiên
                int STT = int.Parse(cap[0].Split(',')[0]);
                string TraLoi = cap[0].Split(',')[1];

                if (QuestionList[STT].Answer.ToLower() == TraLoi.ToLower())
                {
                    // Câu thứ 2
                    int STT2 = int.Parse(cap[1].Split(',')[0]);
                    string TraLoi2 = cap[1].Split(',')[1];
                    if (QuestionList[STT2].Answer.ToLower() == TraLoi2.ToLower())
                    {
                        diem += type ;
                    }
                }
            }
            return diem;
        }
    }
}
